package test

import config.MyFile
import org.apache.commons.io.FileUtils
import utils.ExecCommand

import scala.jdk.CollectionConverters._

object RunFpkm extends MyFile {

  def main(args: Array[String]): Unit = {
    val path = "H:\\喜树碱植物基因组数据库网站\\fpkm2\\opu"
    last(path)
  }

  def last(path:String) = {
    val l1 = s"$path\\filter2.txt".readLines.map(_.split("\t")).map(x => (x.head, x(1)))
    val l2 = l1.map(x => (x._2, x._1))
    val row = (l1 ++ l2).groupBy(_._1).toSeq.map{x=>
      x._1 +"\t" + x._2.map(_._2).mkString(",")
    }.toBuffer
    FileUtils.writeLines(s"$path\\filter_result.txt".toFile, row.asJava)

  }

  def filter2(path:String) = {
    val lines = s"$path\\filter.txt".readLines.map(_.split("\t")).filter(x => Math.abs(x(2).toDouble) >= 0.9 && x.head != x(1)).map(_.mkString("\t"))
    FileUtils.writeLines(s"$path\\filter2.txt".toFile, lines.asJava)
  }

  def getResult(path:String) = {
    s"$path\\result".listFile.foreach { x =>
      println(x.getName)
      val lines = (x.getPath + "/pandv.xls").readLines.tail.map(_.replaceAll("\"", "").split("\t").tail).
        map(y => (y.head, y(1), y(2), y.last)).filter(y => y._4.toDouble <= 0.01 && Math.abs(y._3.toDouble) >= 0.8).map(y => Seq(y._1, y._2, y._3, y._4).mkString("\t"))
      FileUtils.writeLines(s"$path\\filter.txt".toFile, lines.asJava, true)
    }

  }

  def igcAndItc(paths:String) = {
    val files = s"$paths\\grouped".listFile.zipWithIndex
    val path = s"$paths\\result"

    val igc = files.map { x =>
      val file = s"$path/${x._2}_vs_${x._2}"
      (x._1.getPath, x._1.getPath, file)
    }

    igc.grouped(igc.length / 12).foreach { x =>
      x.foreach { y =>
        y._3.mkdirs
        val exec = new ExecCommand
        val cmd = s"Rscript D:/喜树碱植物基因组数据库网站/fpkm/cor_pvalue_calculate.R  -i1 ${y._1} -o ${y._3} -m pearson"
        exec.exect(Array(cmd), y._3)
        println(y._3)
      }
    }

    val itc = files.flatMap { x =>
      files.drop(x._2 + 1).map { y =>
        val file2 = s"$path/${x._2}_vs_${y._2}"
        (x._1.getPath, y._1.getPath, file2)
      }
    }

    itc.grouped(itc.length / 12).foreach { x =>
      x.foreach { y =>
        y._3.mkdirs
        val exec = new ExecCommand
        val cmd = s"Rscript D:/喜树碱植物基因组数据库网站/fpkm/cor_pvalue_calculate.R  -i1 ${y._1} -i2 ${y._2} -o ${y._3} -m pearson"
        exec.exect(Array(cmd), y._3)
        println(y._3)
      }
    }
  }

  def groupFpkm(path:String) = {
    val lines = s"$path/fpkm_filter2.txt".readLines

    val header = lines.head.split("\t").head + "\t" + lines.head.split("\t").zipWithIndex.map(x => x._2).tail.mkString("\t")

    lines.tail.grouped(200).zipWithIndex.foreach { x =>
      FileUtils.writeLines(s"$path\\grouped/group_${x._2}.txt".toFile, (header +: x._1).map(_.split("\t")).transpose.map(_.mkString("\t")).asJava)
    }
  }

  def filter1(path:String) = {
    val lines = s"$path/opu_fpkm.txt".readLines
    val filter =lines.tail.map(_.split("\t")).filter { x =>
      x.tail.count(_ == "0") == 0 && x.tail.map(_.toDouble).sum > 10.0
    }
    FileUtils.writeLines(s"$path/fpkm_filter2.txt".toFile,(lines.head +: filter.map(_.mkString("\t"))).asJava)
    println(filter.length)
  }
}

